import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:redux/redux.dart';

class ReduxDemo extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _ReduxDemoState();
  }
}

class _ReduxDemoState extends State<ReduxDemo>
    with SingleTickerProviderStateMixin {
  var _tabctr;
  var _store;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _tabctr = TabController(length: 2, vsync: this);
    _store = Store<int>((state, action) {
      if (action > 0) {
        state = state + 1;
      }
      return state;
    }, initialState: 0);
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return StoreProvider<int>(
        store: _store,
        child: Scaffold(
          appBar: AppBar(
            title: Text('ReduxDemo'),
            bottom: TabBar(
              tabs: [
                Tab(
                  text: 'item1',
                ),
                Tab(
                  text: 'item2',
                ),
              ],
              controller: _tabctr,
            ),
          ),
          body: TabBarView(
            children: [_FirstPage(), _SecondPage()],
            controller: _tabctr,
          ),
        ));
  }
}

class _FirstPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return StoreConnector<int, Store<int>>(
      converter: (store) => store,
      builder: (context, store){
        return Center(
          child: Column(
            children: <Widget>[
              Text(
                'count = ${store.state}',
                style: Theme.of(context).textTheme.headlineMedium,
              ),
              Text(
                '',
              ),
              Padding(
                padding: EdgeInsets.all(30),
                child: FloatingActionButton(
                  onPressed: () => store.dispatch(1),
                  child: Icon(Icons.add),
                ),
              )
            ],
          ),
        );
      },
    );
  }
}

class _SecondPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return __SecondPageState();
  }
}

class __SecondPageState extends State<_SecondPage> {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return StoreConnector<int, Store<int>>(
      converter: (store) => store,
      builder: (context, store){
        return Center(
          child: Column(
            children: <Widget>[
              Text(
                'count = ${store.state}',
                style: Theme.of(context).textTheme.headlineMedium,
              ),
              Text(
                '',
              ),
              Padding(
                padding: EdgeInsets.all(30),
                child: FloatingActionButton(
                  onPressed: () => store.dispatch(1),
                  child: Icon(Icons.add),
                ),
              )
            ],
          ),
        );
      },
    );
  }
}
